package mashibing.class3;

import pojo.Node;

/**
 * 把给定值都删除
 */
public class Class3_2_DeleteGivenValue {

    public static Node deleteGivenValue(Node head, int num) {
        while (head != null){
            if(head.value != num){
                break;
            }
            head = head.next;
        }
        Node pre = head;
        Node cur = head;
        while (cur != null){
            if(cur.value == num){
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }

    public static void main(String[] args) {
        Node node = new Node(1, new Node(2, new Node(1, null)));
        System.out.println(node);
        Node deleteGivenValue = deleteGivenValue(node,1);
        System.out.println(deleteGivenValue);
    }
}
